package com.manage.common.handler;

import com.manage.common.result.Result;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(AuthenticationException.class)
    public Result<?> handleAuthenticationException(AuthenticationException e) {
        if (e instanceof BadCredentialsException) {
            return new Result().fail("用户名或密码错误");
        } else if (e instanceof DisabledException) {
            return new Result().fail("账户已被禁用");
        }
        return new Result().fail("认证失败: " + e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    public Result<?> handleException(Exception e) {
        return new Result().fail("服务器错误: " + e.getMessage());
    }
}
